package practice1_100;

import java.util.Stack;

public class Practice20 {
    Stack<Character> stack=new Stack<>();

    public boolean isValid(String s) {
        if(s.length()==0) return true;
        //stack.push(s.charAt(0));

        //循环字符串，最后判断栈是否为空，为空为true，否则false
        for (int i=0;i<s.length();i++){
            if(stack.isEmpty()) {//循环中，栈为空就进站操作
                stack.push(s.charAt(i));
            }else if(stack.peek()=='(' && s.charAt(i)==')'){
                stack.pop();
            }else if(stack.peek()=='[' && s.charAt(i)==']'){
                stack.pop();
            }else if(stack.peek()=='{' && s.charAt(i)=='}'){
                stack.pop();
            }else {//不符合上述匹配也进站
                stack.push(s.charAt(i));
            }
        }
        if (stack.isEmpty()) return  true;
        return false;
    }

    public static void main(String[] args) {
        String s="()[]{}";
        Practice20 practice20=new Practice20();
        practice20.isValid(s);
    }
}
